package com.woniuxy.shop.servelet.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.woniuxy.shop.entity.CateGory;
import com.woniuxy.shop.entity.Goods;
import com.woniuxy.shop.exception.ServiceException;
import com.woniuxy.shop.service.CateGoryService;
import com.woniuxy.shop.service.GoodsService;
import com.woniuxy.shop.service.ServiceProxyFactory;

/**
 * Servlet implementation class CateGoryServelet
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	GoodsService gs = ServiceProxyFactory.getProxy(GoodsService.class);
	CateGoryService cs = new CateGoryService();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//查询商品类别，热门商品，最新商品
				try {
					List<CateGory> categories = cs.getCateGory();
					for (CateGory cateGory : categories) {
						List<Goods> goodes = gs.getGoodsByCateGory(cateGory.getId());
						cateGory.setGoodses(goodes);
					}
					List<Goods> hotGoods = gs.getHotGoods();
					List<Goods> newGoods = gs.getNewGoods();
					request.setAttribute("hotGoods", hotGoods);
					request.setAttribute("categories", categories);
					request.setAttribute("newGoods", newGoods);
					request.getRequestDispatcher("index.jsp").forward(request, response);
				} catch (ServiceException e) {
					// TODO: handle exception
				}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
